package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.RecursoHumano;

public class RecursoHumanoDAOImpl extends JdbcDaoSupport implements IRecursoHumanoDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM OPE_RECURSO_HUMANO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(RecursoHumano recursoHumano) {
		deleteOperation.execute( recursoHumano  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public RecursoHumano findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(RecursoHumano recursoHumano) {
		insertOperation.execute( recursoHumano );
	}

	public void update(RecursoHumano recursoHumano) {
		updateOperation.execute( recursoHumano );
	}

	public RecursoHumano findByCriteria(RecursoHumano  recursoHumano) {
		return findByCriteriaQuery.runQuery(recursoHumano);
	}

	public List load(RecursoHumano recursoHumano) {
		return findAllQuery.runQuery(recursoHumano);
	}

	public RecursoHumano findByActiveState(RecursoHumano  recursoHumano) {
		return findByActiveStateQuery.runQuery(recursoHumano);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );    	
    	findByCriteriaQuery = new FindByCriteriaQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO OPE_RECURSO_HUMANO_T ("
				  + " ID, "
				  + " ID_AREA, "
				  + " ID_TIPO_IDENTIFICACION, "
				  + " IDENTIFICACION, "
				  + " CODIGO, "
				  + " NOMBRE_COMPLETO, "
				  + " TELEFONO, "
				  + " NUMERO_CELULAR, "
				  + " EMAIL, "
				  + " SEXO, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " OPE_RECURSO_HUMANO_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			compile();
		}
		public void execute(RecursoHumano recursoHumano) {
			InsertOperation.this.update( new Object[]{				
				recursoHumano.getIdArea(),
				recursoHumano.getIdTipoIdentificacion(),
				recursoHumano.getIdentificacion(),
				recursoHumano.getCodigo(),
				recursoHumano.getNombreCompleto(),
				recursoHumano.getTelefono(),
				recursoHumano.getNumeroCelular(),
				recursoHumano.getEmail(),
				recursoHumano.getSexo(),
				recursoHumano.getEstado(),
				recursoHumano.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE OPE_RECURSO_HUMANO_T SET "					  
					  + " ID_AREA = ? ,"
					  + " ID_TIPO_IDENTIFICACION = ? ,"
					  + " IDENTIFICACION = ? ,"
					  + " CODIGO = ? ,"
					  + " NOMBRE_COMPLETO = ? ,"
					  + " TELEFONO = ? ,"
					  + " NUMERO_CELULAR = ? ,"
					  + " EMAIL = ? ,"
					  + " SEXO = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE"
				  	  + WHERE
				  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(RecursoHumano recursoHumano) {
			UpdateOperation.this.update( new Object[]{			
				recursoHumano.getIdArea(),
				recursoHumano.getIdTipoIdentificacion(),
				recursoHumano.getIdentificacion(),
				recursoHumano.getCodigo(),
				recursoHumano.getNombreCompleto(),
				recursoHumano.getTelefono(),
				recursoHumano.getNumeroCelular(),
				recursoHumano.getEmail(),
				recursoHumano.getSexo(),
				recursoHumano.getEstado(),
				recursoHumano.getUsuarioModifica(),
				recursoHumano.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_RECURSO_HUMANO_T "
										+ WHERE + "1=1";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(RecursoHumano recursoHumano) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM OPE_RECURSO_HUMANO_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			RecursoHumano recursoHumano = new RecursoHumano();
				recursoHumano.setId(new Long(rs.getLong("ID")));
				recursoHumano.setIdArea(new Long(rs.getLong("ID_AREA")));
				recursoHumano.setIdAreaStr(rs.getString("ID_AREA"));
				recursoHumano.setIdTipoIdentificacion(new Long(rs.getLong("ID_TIPO_IDENTIFICACION")));
				recursoHumano.setIdTipoIdentificacionStr(rs.getString("ID_TIPO_IDENTIFICACION"));
				recursoHumano.setIdentificacion(rs.getString("IDENTIFICACION"));
				recursoHumano.setCodigo(rs.getString("CODIGO"));
				recursoHumano.setNombreCompleto(rs.getString("NOMBRE_COMPLETO"));
				recursoHumano.setTelefono(rs.getString("TELEFONO"));
				recursoHumano.setNumeroCelular(rs.getString("NUMERO_CELULAR"));
				recursoHumano.setEmail(rs.getString("EMAIL"));
				recursoHumano.setSexo(rs.getString("SEXO"));
				recursoHumano.setEstado(rs.getString("ESTADO"));
				recursoHumano.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				recursoHumano.setFechaCrea(rs.getDate("FECHA_CREA"));		
				recursoHumano.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				recursoHumano.setFechaModifica(rs.getDate("FECHA_MODIFICA"));	
				recursoHumano.setTipoDto(rs.getString("TIPO_DTO"));
				recursoHumano.setArea(rs.getString("AREA"));	
			return recursoHumano;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public RecursoHumano runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (RecursoHumano) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
    	private static final String sql = "SELECT RH.*,ADM.VRT_NOMBRE AS TIPO_DTO, ADM1.VRT_NOMBRE AS AREA"
			+ " FROM OPE_RECURSO_HUMANO_T RH INNER JOIN "
			+ " ADM_VALOR_TIPO_MAESTRA_T ADM ON RH.ID_TIPO_IDENTIFICACION=ADM.ID_VALOR_TIPO INNER JOIN "
			+ " ADM_VALOR_TIPO_MAESTRA_T ADM1 ON RH.ID_AREA = ADM1.ID_VALOR_TIPO "
										+ WHERE 			
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_AREA = ? "
			  	  	  					+ " ID_TIPO_IDENTIFICACION = ? "
			  	  	  					+ " AND IDENTIFICACION = ? ";
			  	  	  					//+ " AND NOMBRE_COMPLETO = ? "
			  	  	  					//+ " AND TELEFONO = ? "
			  	  	  					//+ " AND NUMERO_CELULAR = ? "
			  	  	  					//+ " AND EMAIL = ? "
			  	  	  					//+ " AND SEXO = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			declareParameter( new SqlParameter(Types.CHAR) );
			declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public RecursoHumano runQuery(RecursoHumano recursoHumano) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//recursoHumano.getId(),
				//recursoHumano.getIdArea(),
				recursoHumano.getIdTipoIdentificacionStr(),
				recursoHumano.getIdentificacion()
				//recursoHumano.getNombreCompleto(),
				//recursoHumano.getTelefono(),
				//recursoHumano.getNumeroCelular(),
				//recursoHumano.getEmail(),
				//recursoHumano.getSexo(),
				//recursoHumano.getEstado(),
				//recursoHumano.getUsuarioCrea(),
				//recursoHumano.getFechaCrea(),
				//recursoHumano.getUsuarioModifica(),
				//recursoHumano.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (RecursoHumano) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = "SELECT RH.*,ADM.VRT_NOMBRE AS TIPO_DTO, ADM1.VRT_NOMBRE AS AREA"
										+ " FROM OPE_RECURSO_HUMANO_T RH INNER JOIN "
										+ " ADM_VALOR_TIPO_MAESTRA_T ADM ON RH.ID_TIPO_IDENTIFICACION=ADM.ID_VALOR_TIPO INNER JOIN "
										+ " ADM_VALOR_TIPO_MAESTRA_T ADM1 ON RH.ID_AREA = ADM1.ID_VALOR_TIPO "
										+ " WHERE (ID_AREA=? OR '%'=?) AND (ID_TIPO_IDENTIFICACION=? OR '%'=? ) AND (IDENTIFICACION =? OR '%'=?  )  ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(RecursoHumano recursoHumano) {
			List result =  execute( new Object[]{
					recursoHumano.getIdArea(),
					recursoHumano.getIdAreaStr(),
					recursoHumano.getIdTipoIdentificacion(),
					recursoHumano.getIdTipoIdentificacionStr(),
					recursoHumano.getIdentificacion(),															
					recursoHumano.getIdentificacion()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public RecursoHumano runQuery(RecursoHumano recursoHumano) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (RecursoHumano) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
